#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2022/1/6 9:41
    Desc  :
--------------------------------------
"""
from django_http_log.serializers import DynamicFieldsModelSerializer
from rest_framework import serializers

from person.models import Person
from person.models.project import Project, ProjectPerson
from person.serializers.person import PersonListSerializer


class ProjectSerializer(DynamicFieldsModelSerializer):
    """项目基础信息序列化"""

    class Meta:
        model = Project
        fields = '__all__'


class ProjectListSerializer(DynamicFieldsModelSerializer):
    """项目列表序列化"""

    projectPerson: int = serializers.SerializerMethodField()

    class Meta:
        model = Project
        fields = '__all__'

    def get_projectPerson(self, obj):
        if obj:
            queryset = ProjectPerson.objects.filter(projectId = obj.id, deleted = False)
            personIds = []
            for i in queryset:
                personIds.append(i.personId)
            queryset_person = Person.objects.filter(id__in = personIds, deleted = False)
            return PersonListSerializer(queryset_person, many = True).data
        return []